// pages/shop-details-seckill/seckill.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vertical: false,
    autoplay: false,
    interval: 3000,
    duration: 500,
    circular: true,
    id: '',
    details: '',
    time: '',
    shi: '',
    fen: '',
    miao: '',
    comment: '',
    publishshow: false,
    focus: false,
    value: '',
    timer1:""
  },
  // 获取商品详情
  getdetails: function () {
    ajax('/api/index/good_show', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        id: this.data.id
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      var arr = res.data
      var li1 = '.jpg', li2 = '.jpeg', li3 = '.png', li4 = '.gif';
      arr.plan_arrs = []
      if (arr.plan_arr) {
        for (var a in arr.plan_arr) {
          if (arr.plan_arr[a].includes(li1) || arr.plan_arr[a].includes(li2) || arr.plan_arr[a].includes(li3) || arr.plan_arr[a].includes(li4)) {
            arr.plan_arrs[a] = { 'url': arr.plan_arr[a], 'style': '0' }
          } else {
            arr.plan_arrs[a] = { 'url': arr.plan_arr[a], 'style': '1' }
          }
        }
      }
      this.setData({
        details: res.data,
        time: res.data.diff_time
      })
      this.altertime()
    })
  },
  // 评论记录
  getcomment: function () {
    ajax('/api/index/forum_show_comment', {
      data: {
        type: 3,
        id: this.data.id,
        page: 1,
        limit: 1
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {

      this.setData({
        comment: res.data
      })
    })
  },
  // 查看更多评论
  seemore: function (e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/comment-list/list?id=' + id,
    })
  },
  // 收藏 
  shoucang: function () {
    ajax('/api/user/favorite_main', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        goods_id: this.data.id
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "put",
    }, res => {
      this.getdetails()
    })
  },

  // 分隔  时分秒  剩余时间  
  altertime: function () {
    var time = this.data.time;
    var arr = time.split(":");



    this.setData({
      shi: arr[0],
      fen: arr[1],
      miao: arr[2]
    })
    this.settime()
  },


  // 定时器  一秒走一次
  settime: function () {
    var that = this
    that.data.timer1 = setInterval(function () {
      var miao = that.data.miao -= 1
      var fen = that.data.fen
      var shi = that.data.shi
      // console.log(miao, fen, shi, 'shifenmiao')
      if (miao <= 0) {
        fen -= 1
        miao = 60
      }
      if (fen == -1) {
        shi -= 1
        fen = 60
      }

      that.setData({
        miao: miao,
        fen: fen,
        shi: shi
      })
      if (shi == -1) {
        clearInterval(that.data.timer1)
        console.log("计时结束")
        setTimeout(function () {

          that.getdetails()

        }, 2000)
      }
    }, 1000)
  },




  // 一级评论
  pinglun: function () {
    this.setData({
      publishshow: true
    });
  },
  // 获取输入框内容
  inputvalue: function (e) {
    this.setData({
      value: e.detail.value,
      focus: true,
    });
  },
  // 发表1级
  fabiaos: function () {
    ajax('/api/user/comment_first_add', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        type: 3,
        zid: this.data.id,
        content: this.data.value
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "post",
    }, res => {
      this.getcomment()
      this.setData({
        publishshow: false,
        focus: false,
        value: '',
      })
    })
  },
  // 立即抢购
  qianggou:function(e){

    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/sure-order/order?id=' + id,
    })


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    this.getdetails()
    this.getcomment()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})